package com.xiaolin.admin.controller;

import com.github.pagehelper.PageInfo;
import com.xiaolin.admin.annotations.HasApi;
import com.xiaolin.admin.constants.ApiPermConstant;
import com.xiaolin.admin.model.entity.SysRole;
import com.xiaolin.admin.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author xiaolin
 * @since 2024-07-29
 */
@Api("角色资源")
@RestController
@RequestMapping("/role")
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;

    @HasApi({ApiPermConstant.COMMON_PERM})
    @ApiOperation("获取所有角色")
    @PostMapping("/getList")
    public List<SysRole> getList(@RequestBody SysRole role) {
        return roleService.getList(role);
    }

    @HasApi({"role:home"})
    @ApiOperation("获取角色列表")
    @PostMapping("/getPage/{page}/{pageSize}")
    public PageInfo<SysRole> getRolePage(@PathVariable Integer page,
                                         @PathVariable Integer pageSize,
                                         @RequestBody SysRole role) {
        return roleService.getRolePage(page, pageSize, role);
    }

    @HasApi({"role:add"})
    @ApiOperation("添加角色")
    @PostMapping("/add")
    public boolean addRole(@RequestBody SysRole role) {
        return roleService.addRole(role);
    }

    @HasApi({"role:get"})
    @ApiOperation("获取角色")
    @PostMapping("/get/{id}")
    public SysRole getRole(@PathVariable("id") Integer id) {
        return roleService.getRole(id);
    }

    @HasApi({"role:update"})
    @ApiOperation("修改角色")
    @PostMapping("/update")
    public boolean updateRole(@RequestBody SysRole role) {
        return roleService.updateRole(role);
    }

    @HasApi({"role:enable"})
    @ApiOperation("启用禁用")
    @PostMapping("/status/{id}/{status}")
    public boolean status(@PathVariable Integer id, @PathVariable String status) {
        return roleService.status(id, status);
    }

    @HasApi({"role:del"})
    @ApiOperation("删除角色")
    @PostMapping("/delete/{id}")
    public boolean deleteRole(@PathVariable Integer id) {
        return roleService.deleteRole(id);
    }


    @HasApi({"role:binding:user"})
    @ApiOperation("给用户配置角色")
    @PostMapping("/configRoleForUsers/{roleId}/{optType}")
    public boolean configRoleForUsers(@PathVariable Integer roleId,
                                      @PathVariable String optType,
                                      @RequestBody List<Integer> userIds) {
        return roleService.configRoleForUsers(roleId, optType, userIds);
    }

    @HasApi({"role:bound:users"})
    @ApiOperation("获取绑定的用户")
    @PostMapping("/getBoundUsers/{roleId}")
    public List<Integer> getBoundUsers(@PathVariable Integer roleId) {
        return roleService.getBoundUsers(roleId);
    }

    @HasApi({"role:binding:menu"})
    @ApiOperation("角色绑定模块")
    @PostMapping("/configMenuForRole/{roleId}")
    public boolean configMenuForRole(@PathVariable Integer roleId, @RequestBody List<Integer> menuIds) {
        return roleService.configMenuForRole(roleId, menuIds);
    }

    @HasApi({"role:bound:menus"})
    @ApiOperation("获取绑定的模块")
    @PostMapping("/getBoundMenus/{roleId}")
    public List<Integer> getBoundMenus(@PathVariable Integer roleId) {
        return roleService.getBoundMenus(roleId);
    }

    @HasApi({"role:binding:org"})
    @ApiOperation("角色绑定组织架构")
    @PostMapping("/configOrgForRole/{roleId}")
    public boolean configOrgForRole(@PathVariable Integer roleId, @RequestBody List<Integer> orgIds) {
        return roleService.configOrgForRole(roleId, orgIds);
    }

    @HasApi({"role:bound:orgs"})
    @ApiOperation("获取绑定的组织架构")
    @PostMapping("/getBoundOrgs/{roleId}")
    public List<Integer> getBoundOrgs(@PathVariable Integer roleId) {
        return roleService.getBoundOrgs(roleId);
    }

}
